#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "BF.h"
//#include "Sorted_GetAllEntries.h"
#include "record.h"

#define BLOCK_SIZE 1024

/*
 * creates and initializes an empty file named 'fileName' 
 * @parameter fileName: the name of the file to be created
 * returns 0 in case of success else -1
 */
int Sorted_CreateFile( char* fileName ) 
{
	void* block;
	int flag;
	int fileDesc;

	
	if( BF_CreateFile(fileName) < 0 )
	{
		BF_PrintError("Cannot Create File");
		return -1;
	}
	
	if( fileDesc = BF_OpenFile(fileName) < 0 )
	{
		BF_PrintError("Cannot Open File");
		return -1;
	}
	
	if( BF_AllocateBlock(fileDesc) < 0 )
	{
		BF_PrintError("Cannot Allocate Block");
		return -1;
	}
		
	if ( BF_ReadBlock(fileDesc, 0, &block) < 0 )
	{
		BF_PrintError("Cannot Read Block");
		return -1;
	}
	
	flag = 0;
	memcpy(block, &flag, sizeof(int));

	if( BF_WriteBlock(fileDesc, 0) < 0 )
	{
		BF_PrintError("Cannot Write Block");
		return -1;
	}
	
	if( BF_CloseFile(fileDesc) < 0 )
	{
		BF_PrintError("Cannot Close File");
		return -1;
	}

	return 0;	
}

/*
 * opens the file 'fileName' and reads from the first block the file descriptor 
 * @parameter fileName: the name of the file to be opened
 * returns the file descriptor in case of success else -1
 */
int Sorted_OpenFile( const char* fileName )
{
	int fileDesc;
	void* block;
	
	if( ( fileDesc = BF_OpenFile(fileName) ) < 0 )
	{
		BF_PrintError("Cannot Open File");
		return -1;
	}
	
	return fileDesc;
}

/*
 * closes the file described by 'fileDesc'
 * @parameter fileDesc: the file descriptor of the file to be closed
 * returns 0 in case of success else -1
 */
int Sorted_CloseFile( int fileDesc )
{
	if( BF_CloseFile(fileDesc) < 0 )
	{
		BF_PrintError("Cannot Close File");
		return -1;
	}
	return 0;
}

/*
 * inserts the first entry 'record' in a sortedfile described by 'fileDesc'
 * @parameter fileDesc: the file descriptor of the file
 * @parameter record: the record to be inserted
 * returns 0 in case of success else -1
 */

int Sorted_InsertFirstEntry( int fileDesc, Record record )
{
	void* block;
	int recordsNum;
	
	if( BF_ReadBlock(fileDesc, 0, &block) < 0 )
	{
			BF_PrintError("Cannot Read Block");
			return -1;
	}
	
	recordsNum = 1;
	memcpy( block, &recordsNum , sizeof(int) );
	memcpy( block + sizeof(int), &record, sizeof(Record) );        
	
	if( BF_WriteBlock(fileDesc,0) < 0 )
	{
			BF_PrintError("Cannot Write Block");
			return -1;
	}
	
	return 0;
}


 /*
 * prints all entries where their field name 'fieldName' is of value 'value' and the blocks read
 * if value is NULL, all entries are printed
 * @parameter fileDesc: the file descriptor of the file
 * @parameter fieldName: the field name to be checked
 * @parameter value: the value of the field name
 */

/*
 * todo: 	binary search for non null 
 */
void Sorted_GetAllEntries( int fileDesc, char* fieldName, void* value )
{
	printf("fieldName: %s\n", fieldName);

	void* block;
	int numberOfBlocks; 
	int blocksRead = 0;
	int blockIndex;
	int recordsNum;	
	Record* currentRecord,tempRecord;
	int recordIndex;
	char* pointer;
	
	/*binary search variables*/
	int fieldNo;
	int bsBlockIndex;
	int bsRecordIndex;
	int bsFound;
	int zeroBlockCounter;

	currentRecord = malloc(sizeof(Record));

	printf("  ID |             N A M E           |         S U R N A M E         |  POINTS\n");
	printf("-----|-------------------------------|-------------------------------|---------\n");


	/*getting number of blocks*/
	if( ( numberOfBlocks = BF_GetBlockCounter(fileDesc) ) < 0 ){
		BF_PrintError("Cannot Get Block Counter");
		return;
	}


	if(value != NULL){
		tempRecord = malloc(sizeof(Record));
		if( !strcmp(fieldName,"id") ){
				fieldNo = 0;
				tempRecord->id = *(int *)value;
		{
		else if( !strcmp(fieldName,"name") ){
				fieldNo = 1;
				tempRecord->name = (char *)value;
		}
		else if( !strcmp(fieldName,"surname") ){
				fieldNo = 2;
				tempRecord->name = (char *)value;
		}
		else if( !strcmp(fieldName,"avgPoints") ){
				fieldNo = 3;
				tempRecord->name = (char *)value;
		}
		blockIndex = numberOfBlocks/2;
	}
	else{
		blockIndex = 0;
	}
	for( blockIndex  ; blockIndex < numberOfBlocks ; blockIndex++  ){
		
		if( zeroBlockCounter > 1 ){
			break;
		}
		if( BF_ReadBlock(fileDesc, blockIndex, &block) < 0 ){
			BF_PrintError("Cannot Read Block");
			printf("Blocks Read: %d\n", blocksRead);
			return;
		}
		blocksRead++;
		
		/*getting the number of records of the block*/
		memcpy( &recordsNum, block, sizeof(int) );

		/*for each record we check the field name and the value*/
		for( recordIndex = 0 ; recordIndex < recordsNum ; recordIndex++ ){

			/*getting the contents of each record of the block*/
			pointer = block+sizeof(int)+recordIndex*sizeof(Record);
			memcpy( currentRecord, pointer, sizeof(Record) );
		
			/*we check the value according to the fieldName input*/
			if (value == NULL) {
				printf("{ %3d|%31s|%31s| %8f}\n",currentRecord->id,currentRecord->name,currentRecord->surname,currentRecord->avgPoints);
			}
			else{
				if( compareEntries(tempRecord,currentRecord) == 1 ){
					
				}
				else if( compareEntries(tempRecord,currentRecord) == 0 ){

				}
				else{

				}
			}
		}	
	}
	
		
		
		

		

	
			

			/*
			else if( !strcmp(fieldName,"id") ){
	
				/*if the record's value matches the input value we print it*/
				/*if not, we move to the next record*\
				if( currentRecord->id == *((int *)value) ){
					printf("{ %3d|%31s|%31s| %8f}\n",currentRecord->id,currentRecord->name,currentRecord->surname,currentRecord->avgPoints);
				}
				else continue;				
	
			}
			else if( !strcmp(fieldName,"name") ){

				if( !strcmp( currentRecord->name , (char* )value ) ){
					printf("{ %3d|%31s|%31s| %8f}\n",currentRecord->id,currentRecord->name,currentRecord->surname,currentRecord->avgPoints);
				}
				else continue;

			}
			else if( !strcmp(fieldName,"surname") ){

				
				if( !strcmp( currentRecord->surname , (char* )value ) ){
					printf("{ %3d|%31s|%31s| %8f}\n",currentRecord->id,currentRecord->name,currentRecord->surname,currentRecord->avgPoints);
				}
				else continue;

			}
			else if( !strcmp(fieldName,"avgPoints") ){

				if( currentRecord->avgPoints == *((float *) value) ) {
					printf("{ %3d|%31s|%31s| %8f}\n",currentRecord->id,currentRecord->name,currentRecord->surname,currentRecord->avgPoints);
				}
				else continue;
			
			}
			else{
				printf("unidentified field name\n");
				printf("Blocks Read: %d\n", blocksRead);
				return;
			}			
		*/
			
				


	
	
	
	printf("Blocks Read: %d\n", blocksRead);
	free(currentRecord);
}

 /*
 * checks if file 'file' is sorted compared to field described by 'fieldNo'
 * @parameter file: the file to be checked
 * @parameter fieldNo: describes the field which the file will be checked upon
 */
int Sorted_checkSortedFile( const char* file, int fieldNo )
{
	int fileDesc, recordsNum, blockNumber, blockIndex, i, j;
	void* block;
	char* pointer;
	Record record1;
	Record record2;
	
	if ( (fieldNo > 3) || (fieldNo < 0) )
	{
		perror("Invalid field number\n");
		return 0;
	}
	
	
	if ( ( fileDesc = BF_OpenFile(file) ) < 0)
	{
		BF_PrintError("Cannot Open File");
		return 0;
	}
	
	if ( (blockNumber = BF_GetBlockCounter(fileDesc)  ) < 0 )
	{ 
		BF_PrintError("Cannot Get Block Count");
		return 0;
	}
	
	/* Read first block */
	if( BF_ReadBlock(fileDesc, 0, &block) < 0 )
	{
		BF_PrintError("Cannot Read Block");
		return 0;
	}
	
	blockIndex = 0;
	/* intialization */
	while(1)
	{
		memcpy( &recordsNum, block, sizeof(int) );
		
		if ( (recordsNum == 0) && (blockNumber == 1) )
			/* A file with one or none records is sorted */
			return 1;
		else if ( (recordsNum == 0) && (blockIndex < blockNumber) )
		{
			blockIndex++;
			if( BF_ReadBlock(fileDesc, blockIndex, &block) < 0 )
			{
				BF_PrintError("Cannot Read Block");
				return 0;
			}
			if (blockIndex == blockNumber)
				/* A file with none records is sorted */
				return 1;
			continue;
		}
		else
		{	
			/* Initialize record1 and record2 for the iteration */
			memcpy( &record1, block + sizeof(int), sizeof(Record) );
			//memcpy( &record2, block + sizeof(int) + sizeof(Record), sizeof(Record) );
			record2 = record1;
		
			break;
		}
	}
	
	pointer = block + sizeof(int);
	
	for( i = blockIndex; i < blockNumber; i++ )
	{
	
		memcpy( &recordsNum, block, sizeof(int) );
		
		for( j = 0; j < recordsNum; j++)
		{
			record1 = record2;
			memcpy( &record2, pointer, sizeof(Record) );
			
			if ( !compare(record1, record2, fieldNo) )
				return 0;
				
			//if (fieldNo == 0)
			//{
				//if (record1.id > record2.id)
					//return 0;
			//}
			//else if (fieldNo == 1)
			//{
				//if ( strcmp(record1.name,record2.name) < 0)
					//return 0;
			//}
			//else if (fieldNo == 2)
			//{
				//if ( strcmp(record1.surname,record2.surname) < 0)
					//return 0;
			//}
			//else if (fieldNo == 3)
			//{
				//if (record1.avgPoints > record2.avgPoints)
					//return 0;
			//}
			
			/* Move the pointer to the next record */
			pointer += sizeof(Record);
		}
		
		if (i < blockNumber - 1)
		{
			if( BF_ReadBlock(fileDesc, i+1, &block) < 0 )
			{
				BF_PrintError("Cannot Read Block");
				return 0;
			}
		}
		pointer = block + sizeof(int);
	}

	return 1;
}

 /*
 * Merges sorted files file1, file2 by field described by fieldNo in also sorted file named "file1file2fieldNo"
 * @parameter file1, file2
 * @parameter fieldNo
 * returns 0 in case of success else -1
 */
int Sorted_mergeFiles( const char* file1, const char* file2, int fieldNo ) 
{
	void* block1;
	void* block2;
	void* mergedBlock;
	void* pointer;
	int fileDesc1, fileDesc2;
	int mergedFileDesc;
	int EndOfFile1, EndOfFile2;
	int blocksNum1, blocksNum2;
	int mergedBlockNum;
	int curBlockNum1, curBlockNum2;
	int recordsNum1, recordsNum2, mergedRecords;
	int currentRecNum1, currentRecNum2; 
	Record record1, record2;
	
	int cmpRet;
	
	/* Check if files are sorted */
	if ( Sorted_checkSortedFile(file1,fieldNo) == 0 )
	{	
		printf("Not Sorted File\n");
		return -1;
	}
		
	if ( Sorted_checkSortedFile(file2,fieldNo) == 0 )
	{
		printf("Not Sorted File\n");
		return -1;
	}
		
	char mergedFileName[32];
	sprintf(mergedFileName, "%s%s%d", file1, file2, fieldNo);
	
	if (Sorted_CreateFile(mergedFileName) < 0)
	{
		printf("Cannot Create File");
		return -1;
	}
	
	/* Open Files */
	if ( (mergedFileDesc = Sorted_OpenFile(mergedFileName)) < 0)
	{
		printf("Cannot Open File");
		return -1;
	}
	
	if ( (fileDesc1 = Sorted_OpenFile(file1)) < 0)
	{
		printf("Cannot Open File");
		return -1;
	}
		
	if ( (fileDesc2 = Sorted_OpenFile(file2)) < 0)
	{
		printf("Cannot Open File");
		return -1;
	}
	
	
	if( BF_AllocateBlock(mergedFileDesc) < 0 )
	{
		BF_PrintError("Cannot Allocate Block");
		return -1;
	}
	
	/* Get numbers of allocated blocks in files */
	if ( (blocksNum1 = BF_GetBlockCounter(fileDesc1) ) < 0 )
	{ 
		BF_PrintError("Cannot Get Block Count");
		return -1;
	}
	
	if ( (blocksNum2 = BF_GetBlockCounter(fileDesc2) ) < 0 )
	{ 
		BF_PrintError("Cannot Get Block Count");
		return -1;
	}
	
	/*
	 * Merge Loop
	 * 
	 * Variables
	 * *********
	 * block(1-2): 		 	is the current file(1-2)'s block
	 * curBlockNum(1-2):	is the current file(1-2)'s block number
	 * recordsNum(1-2):  	is the number of records of file(1-2)'s current block
	 * record(1-2): 	 	is the current block(1-2)'s record
	 * currentRecNum(1-2): 	is the current block(1-2)'s record number
	 * mergedRecords:	 	is the number of written records of current merged file's block
	 * **********
	 */
	
	if( BF_ReadBlock(mergedFileDesc, 0, &mergedBlock) < 0 )
	{
		BF_PrintError("Cannot Read Block");
		return -1;
	}
	
	/* Points where the next record will be written */
	pointer = mergedBlock + sizeof(int);
	
	if( BF_ReadBlock(fileDesc1, 0, &block1) < 0 )
	{
		BF_PrintError("Cannot Read Block");
		return -1;
	}
	
	if( BF_ReadBlock(fileDesc2, 0, &block2) < 0 )
	{
		BF_PrintError("Cannot Read Block");
		return -1;
	}
	
	memcpy( &recordsNum1, block1, sizeof(int) );
	memcpy( &recordsNum2, block2, sizeof(int) );
	
	EndOfFile1 = 0;
	EndOfFile2 = 0;
	currentRecNum1 = 0;
	currentRecNum2 = 0;
	
	int initRec1, initRec2;
	initRec1 = 0;
	initRec2 = 0;
	
	curBlockNum1 = 0;
	curBlockNum2 = 0;
	
	/* Initialize record 1 and record 2 */
	while(1)
	{
		if ( !initRec1 )
		{
			if (recordsNum1 > 0)
			{
				/* Get the first record of files */
				memcpy( &record1, block1 + sizeof(int), sizeof(Record) );
				currentRecNum1 = 1;
				initRec1 = 1;
			}
			else
			{
				curBlockNum1++;
				if (curBlockNum1 == blocksNum1)
				{
					EndOfFile1 = 1;
					initRec1 = 1;
					continue;
				}
				if( BF_ReadBlock(fileDesc1, curBlockNum1, &block1) < 0 )
				{
					BF_PrintError("Cannot Read Block");
					return -1;
				}
				memcpy( &recordsNum1, block1, sizeof(int) );
			}
			
		}
		
		if ( !initRec2 )
		{
			if (recordsNum2 > 0)
			{
				/* Get the first record of files */
				memcpy( &record2, block2 + sizeof(int), sizeof(Record) );
				currentRecNum2 = 1;
				initRec2 = 1;
			}
			else
			{
				curBlockNum2++;
				if (curBlockNum2 == blocksNum2)
				{
					EndOfFile2 = 1;
					initRec2 = 1;
					continue;
				}
				if( BF_ReadBlock(fileDesc2, curBlockNum2, &block2) < 0 )
				{
					BF_PrintError("Cannot Read Block");
					return -1;
				}
				memcpy( &recordsNum2, block2, sizeof(int) );
			}
			
		}
		if (initRec1 && initRec2 )
			break;
	}
	
	mergedRecords = 0;
	mergedBlockNum = 0;
	
	/* Merge Loop */
	while(1)
	{
		
		if ( EndOfFile1 && EndOfFile2) /* there are no more records */
			break;
		
		if ( EndOfFile2 || compare(record1, record2, fieldNo) )	/* record 1 */
		{
			/* check if block has space */
			if ( (mergedRecords*sizeof(Record) + sizeof(int)) <= BLOCK_SIZE - sizeof(Record) )
			{
				/* Write record to block */
				memcpy(pointer, &record1, sizeof(Record));
				pointer += sizeof(Record);
				mergedRecords++;
				memcpy(mergedBlock, &mergedRecords, sizeof(int));
			}
			else /* block has no space */
			{
				memcpy(mergedBlock, &mergedRecords, sizeof(int));
				if( BF_WriteBlock(mergedFileDesc, mergedBlockNum) < 0 )
				{
					BF_PrintError("Cannot Write Block");
					return -1;
				}
				if( BF_AllocateBlock(mergedFileDesc) < 0 )
				{
					BF_PrintError("Cannot Allocate Block");
					return -1;
				}
				/* Read the new block */
				mergedBlockNum++;
				if( BF_ReadBlock(mergedFileDesc, mergedBlockNum, &mergedBlock) < 0 )
				{
					BF_PrintError("Cannot Read Block");
					return -1;
				}

				pointer = mergedBlock + sizeof(int);
				memcpy(pointer, &record1, sizeof(Record));
				pointer += sizeof(Record);
				mergedRecords++;
				memcpy(mergedBlock, &mergedRecords, sizeof(int));
			}
			
			/* check if it is the last record of block */
			if (currentRecNum1 == recordsNum1)
			{
				/* if there are more blocks to read */
				if (curBlockNum1 < blocksNum1 )
				{
					curBlockNum1++;
					if( BF_ReadBlock(fileDesc1, curBlockNum1, &block1) < 0 )
					{
						BF_PrintError("Cannot Read Block");
						return -1;
					}
					/* get the new block's record number */
					memcpy( &recordsNum1, block1, sizeof(int) );
					
					/* get first record */
					memcpy( &record1, block1 + sizeof(int), sizeof(Record) );
					currentRecNum1 = 1;
				}
				else /* file has no more records! */
				{
					EndOfFile1 = 1;
				}
			}
			else
			{
				/* get next record */
				memcpy( &record1, block1 + sizeof(int) + currentRecNum1*sizeof(Record), sizeof(Record) );
				currentRecNum1++;
			}
		}
		else /* record 2 */
		{
			/* check if block has space */
			if ( (mergedRecords*sizeof(Record) + sizeof(int)) <= BLOCK_SIZE - sizeof(Record) )
			{
				/* Write record to block */
				memcpy(pointer, &record2, sizeof(Record));
				pointer += sizeof(Record);
				mergedRecords++;
				memcpy(mergedBlock, &mergedRecords, sizeof(int));
			}
			else /* block has no space */
			{
				memcpy(mergedBlock, &mergedRecords, sizeof(int));
				if( BF_WriteBlock(mergedFileDesc, mergedBlockNum) < 0 )
				{
					BF_PrintError("Cannot Write Block");
					return -1;
				}
				if( BF_AllocateBlock(mergedFileDesc) < 0 )
				{
					BF_PrintError("Cannot Allocate Block");
					return -1;
				}
				/* Read the new block */
				mergedBlockNum++;
				if( BF_ReadBlock(mergedFileDesc, mergedBlockNum, &mergedBlock) < 0 )
				{
					BF_PrintError("Cannot Read Block");
					return -1;
				}

				pointer = mergedBlock + sizeof(int);
				memcpy(pointer, &record2, sizeof(Record));
				pointer += sizeof(Record);
				mergedRecords++;
				memcpy(mergedBlock, &mergedRecords, sizeof(int));
			}
			
			/* check if it is the last record of block */
			if (currentRecNum2 == recordsNum2)
			{
				/* if there are more blocks to read */
				if (curBlockNum2 < blocksNum2 )
				{
					curBlockNum2++;
					if( BF_ReadBlock(fileDesc2, curBlockNum2, &block2) < 0 )
					{
						BF_PrintError("Cannot Read Block");
						return -1;
					}
					/* get the new block's record number */
					memcpy( &recordsNum2, block2, sizeof(int) );
					
					/* get first record */
					memcpy( &record2, block2 + sizeof(int), sizeof(Record) );
					currentRecNum2 = 1;
				}
				else /* file has no more records! */
				{
					EndOfFile2 = 1;
				}
			}
			else
			{
				/* get next record */
				memcpy( &record2, block2 + sizeof(int) + currentRecNum2 * sizeof(Record), sizeof(Record) );
				currentRecNum2++;
			}
		}
	}
	
	Sorted_CloseFile(fileDesc2);
	Sorted_CloseFile(fileDesc1);
	Sorted_CloseFile(mergedFileDesc);
	
	return 0;
}

int compare( Record record1, Record record2, int fieldNo )
{
	if ( (fieldNo > 3) || (fieldNo < 0) )
		return -1;
	
	if (fieldNo == 0)
	{
		if (record1.id <= record2.id)
			return 1;
		return 0;
	}
	else if (fieldNo == 1)
	{
		if ( strcmp(record1.name,record2.name) <= 0)
			return 1;
		return 0;
	}
	else if (fieldNo == 2)
	{
		if ( strcmp(record1.surname,record2.surname) <= 0)
			return 1;
		return 0;
	}
	else if (fieldNo == 3)
	{
		if (record1.avgPoints >= record2.avgPoints)
			return 1;
		return 0;
	}
}

int compareEntries( Record record1, Record record2, int fieldNo )
{
	if ( (fieldNo > 3) || (fieldNo < 0) )
		return -1;
	
	if (fieldNo == 0)
	{
		if (record1.id < record2.id)
			return 1;
		else if (record1.id == record2.id)
			return 0;
		return -1;
	}
	else if (fieldNo == 1)
	{
		if ( strcmp(record1.name,record2.name) < 0)
			return 1;
		else if ( strcmp(record1.name,record2.name) == 0)
			return 0;
		return -1;
	}
	else if (fieldNo == 2)
	{
		if ( strcmp(record1.surname,record2.surname) < 0)
			return 1;
		else if ( strcmp(record1.surname,record2.surname) == 0)
			return 0;
		return -1;
	}
	else if (fieldNo == 3)
	{
		if (record1.avgPoints > record2.avgPoints)
			return 1;
		else if (record1.avgPoints == record2.avgPoints)
			return 0;
		return -1;
	}
}
//int Sorted_InsertFirstEntry( int fileDesc, Record record )
//{
	//void* block;
	//void* pointer;
	//int recordsNum;
	//Record recWrite;
	//Record recRead;
	//int numberOfBlocks;
	
	//int i,j;
	
	//if( ( numberOfBlocks = BF_GetBlockCounter(fileDesc) ) < 0 ){
		//BF_PrintError("Cannot Get Block Counter");
		//return -1;
	//}
	
	//recWrite = record;
	
	//for (i =0; i<numberOfBlocks; i++)
	//{
		//if( BF_ReadBlock(fileDesc, i, &block) < 0 )
		//{
			//BF_PrintError("Cannot Read Block");
			//return -1;
		//}
		
		//pointer = block + sizeof(int);
		//memcpy( &recordsNum, block, sizeof(int) );
		
		//for (j = 0; j<recordsNum; j++)
		//{
			//memcpy( &recRead, pointer, sizeof(Record) );
			
			//memcpy( block + sizeof(int), &recWrite, sizeof(Record) );
			////memcpy( pointer, &recWrite, sizeof(Record) );
			
			//recWrite = recRead;
			//pointer += sizeof(Record);
		//}
		
		//if( BF_WriteBlock(fileDesc, i) < 0 )
		//{
			//BF_PrintError("Cannot Write Block");
			//return -1;
		//}
		 
	//}
	
	///* shift of the last record */
	//if ( (recordsNum * sizeof(Record) + sizeof(int)) <= BLOCK_SIZE - sizeof(Record) )
	//{
		//if( BF_AllocateBlock(fileDesc) < 0 )
		//{
			//BF_PrintError("Cannot Allocate Block");
			//return -1;
		//}
		//numberOfBlocks++;
		//if( BF_ReadBlock(fileDesc, numberOfBlocks - 1, &block) < 0 )
		//{
			//BF_PrintError("Cannot Read Block");
			//return -1;
		//}
		
		//pointer = block + sizeof(int);
		//memcpy( pointer, &recWrite, sizeof(Record) );
		//int flag = 1;
		//memcpy( block, &flag, sizeof(int) );
		
		//if( BF_WriteBlock(fileDesc, numberOfBlocks - 1) < 0 )
		//{
			//BF_PrintError("Cannot Write Block");
			//return -1;
		//}
		
	//}
	//else
	//{
		//memcpy( pointer, &recWrite, sizeof(Record) );
		//int flag = recordsNum++;
		//memcpy( block, &flag, sizeof(int) );
		//if( BF_WriteBlock(fileDesc, numberOfBlocks - 1) < 0 )
		//{
			//BF_PrintError("Cannot Write Block");
			//return -1;
		//}
	//}
	
	//return 0;
//}
